from django.db import models
from django.conf import settings


class UserStats(models.Model):
    """用户统计数据模型"""
    
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, 
        on_delete=models.CASCADE, 
        related_name='stats',
        db_comment='用户ID'
    )
    total_artworks = models.IntegerField(
        default=0,
        db_comment='总作品数'
    )
    painting_count = models.IntegerField(
        default=0,
        db_comment='绘画作品数'
    )
    music_count = models.IntegerField(
        default=0,
        db_comment='音乐作品数'
    )
    writing_count = models.IntegerField(
        default=0,
        db_comment='写作作品数'
    )
    total_views = models.IntegerField(
        default=0,
        db_comment='总浏览量'
    )
    total_likes = models.IntegerField(
        default=0,
        db_comment='总点赞量'
    )
    total_comments = models.IntegerField(
        default=0,
        db_comment='总评论数'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        db_comment='更新时间'
    )
    
    class Meta:
        db_table = 'creation_user_stats'
        db_table_comment = '用户统计数据表'
        verbose_name = '用户统计'
        verbose_name_plural = '用户统计管理'
        indexes = [
            models.Index(fields=['user']),
        ]
    
    def __str__(self):
        return f"{self.user.username} 的统计数据"


class DailyStats(models.Model):
    """每日统计数据模型"""
    
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, 
        on_delete=models.CASCADE, 
        related_name='daily_stats',
        db_comment='用户ID'
    )
    stat_date = models.DateField(
        db_comment='统计日期'
    )
    view_count = models.IntegerField(
        default=0,
        db_comment='当日浏览量'
    )
    like_count = models.IntegerField(
        default=0,
        db_comment='当日点赞量'
    )
    comment_count = models.IntegerField(
        default=0,
        db_comment='当日评论数'
    )
    artwork_count = models.IntegerField(
        default=0,
        db_comment='当日创作数'
    )
    
    class Meta:
        db_table = 'creation_daily_stats'
        db_table_comment = '每日统计数据表'
        verbose_name = '每日统计'
        verbose_name_plural = '每日统计管理'
        unique_together = ['user', 'stat_date']
        ordering = ['-stat_date']
        indexes = [
            models.Index(fields=['user', 'stat_date']),
        ]
    
    def __str__(self):
        return f"{self.user.username} - {self.stat_date}"
